package com.zhaolang.qogir.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.bean.OrderFinishedBean;
import com.zhaolang.qogir.ui.config.OrderConstants;

import java.util.List;

public class FragmentProcessFinishedAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private Context mContext;
    private List<OrderFinishedBean> mData;
    private final String unkonwn = "未知";

    public FragmentProcessFinishedAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<OrderFinishedBean> mData){
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public OrderFinishedBean getItem(int position) {
        return mData != null && position >= 0 & position < mData.size() ? mData
                .get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final OrderFinishedBean bean = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ui_processed_new, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(bean.getName() != null){
            holder.name.setText(bean.getName());
        }else{
            holder.name.setText(unkonwn);
        }
        if(bean.getAddress() != null){
            holder.address.setText(bean.getAddress());
        }else{
            holder.address.setText(unkonwn);
        }
        if(bean.getTel() != null){
            holder.tel.setText(bean.getTel());
        }else{
            holder.tel.setText(unkonwn);
        }
        if(bean.getTime() != null){
            holder.time.setText(bean.getTime());
        }else{
            holder.time.setText(unkonwn);
        }
        if(bean.getOrderStatus() == OrderConstants.ORDER_STATUS_DELIVERY_COMPLETE){
            holder.btnObtain.setTextColor(mContext.getResources().getColor(R.color.black));
            holder.btnObtain.setText(R.string.order_status_finished);
        }else{
            holder.btnObtain.setTextColor(mContext.getResources().getColor(R.color.blue));
            holder.btnObtain.setText(R.string.order_status_complete);
        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    private class ViewHolder {
        private TextView name;
        private TextView tel;
        private TextView address;
        private TextView time;
        private TextView btnObtain;
        ViewHolder(View view) {
            name = view.findViewById(R.id.name);
            tel = view.findViewById(R.id.tel);
            address = view.findViewById(R.id.address);
            time = view.findViewById(R.id.time);
            btnObtain = view.findViewById(R.id.btn_obtain);
        }
    }
}
